<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cake.dao.OrderDetailDao"%>
<%@page import="cake.db.MySqlDb"%>
<%@page import="java.sql.ResultSet"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Administrator Order Management page</title>
		<link rel="stylesheet" href="styles/style.css" type="text/css" />
		<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
		<script src="scripts/jquery.select.js" type="text/javascript"></script>
		<script src="scripts/tools.js" type="text/javascript"></script>
		<script src="scripts/script.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="container">
			<div id="header">
				<div style="padding-top: 50px;">
					<div class=" btn" style="margin-left: 50px;"> <%
 	String manager = null;
 	long managerId = 0;
 	manager = (String) session.getAttribute("managername");
 	managerId = (Long) session.getAttribute("managerid");
 	if (manager == null || manager.equals("")) {
 		response.sendRedirect("admin.jsp");
 	} else {
 %> Welcome: <%=manager%> 
						 <%
 	}
 %> </div>
 	<div style="margin-left:500px;">
 		<span style="margin-left:10px;cursor:pointer" onclick="toURL('orders.jsp');">Orders</span>
 		<span style="margin-left:10px;cursor:pointer" onclick="toURL('adminComments.jsp');">Comments</span>
 		<span style="margin-left:10px;cursor:pointer" onclick="toURL('adminCakes.jsp');">Cakes</span>
 	</div>
 	<div class="pointer" style="margin-left:900px;" onclick="adminQuit();">Quit</div>
				</div>
				
			</div>
			<div id="content">
				<div style="margin-left: 1px;">
					<div style="padding: 40px 5px 40px 5px;">
						<div  style="min-height:500px;">
							<span id="switch1" class="switch_btn1" onclick="showOrderList();">Order List</span><span id="switch2" class="switch_btn2" onclick="showDiyList();">DIY List</span>
							<div id="orderList">
							<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" bordercolor="#CCCCCC">
								<tr style="background-color:#FCF2E4;">
									<td width="30">
										Index
									</td>
									<td width="150">
										Code
									</td>
									<td width="65">
										Customer
									</td>
									<td width="60">
										Amount
									</td>
									<td width="200">
										Cake
									</td>
									<td width="130">
										Address
									</td>
									<td width="90">
										Phone
									</td>
									<td width="100">
										Remark
									</td>
									<td width="60">
										Status
									</td>
									<td width="110">
										Operation
									</td>
								</tr>
								<%
									String currPage = request.getParameter("page");
									int pageSize = 20;
									int pageNum = 1;
									if (currPage != null && !currPage.equals("")) {
										pageNum = Integer.parseInt(currPage);
									}
									MySqlDb db = new MySqlDb();

									String sql = "select count(*) from myorder";

									ResultSet rs = db.query(sql);

									int total = 0;
									while (rs.next()) {
										total = rs.getInt(1);
										break;
									}
									int totalPage = 0;
									if (total % pageSize == 0) {
										totalPage = total / pageSize;
									} else {
										totalPage = total / pageSize + 1;
									}

									sql = "select user.username,myorder.*,cake.name,cake.price,cake.picture,orderdetail.quantity from myorder "
											+ " left outer join user on myorder.userid=user.id"
											+ " left outer join orderdetail on myorder.id=orderdetail.orderid left outer join cake "
											+ " on orderdetail.cakeid=cake.id"
											+ " order by status asc limit "
											+ (pageNum - 1)
											* pageSize
											+ ","
											+ pageSize;

									rs = db.query(sql);
									int index = 1;
									while (rs.next()) {
								%>
								<tr>
									<td><%=index%></td>
									<td><%=rs.getString("code")%></td>
									<td><%=rs.getString("username")%></td>
									<td><%=rs.getFloat("amount")%></td>
									<td>
										<img src="<%=rs.getString("picture")%>"
											alt="<%=rs.getString("name")%>"
											style="float: left; width: 50px; height: 50px;" />
										<b>name:</b><%=rs.getString("name")%><br/>
										<b>price:</b><%=rs.getFloat("price")%>
									</td>
									<td><%=rs.getString("address")%></td>
									<td><%=rs.getString("phone")%></td>
									<td><%=rs.getString("remark")%></td>
									<td>
										<%
											int status = rs.getInt("status");
												String temp = "";
												String operation="";
												switch (status) {
													case 0 :
														temp = "ordered";
														operation = "<span class='pointer' onclick='sendOrder("+rs.getLong("id")+");'>send</span>"
																+" | <span class='pointer' onclick='cancelOrderByAdmin("+rs.getLong("id")+");'>cancel</span>";
														break;
													case 1 :
														temp = "sended";
														break;
													case 2 :
														temp = "successed";
														break;
													case 3 :
														temp = "failed";
														break;
												}
										%>
										<%=temp%>
									</td>
									<td>
										<%=operation %>
									</td>
								</tr>
								<%
									index++;
									}
								%>
							</table>
							<div>
								<%
									if (totalPage > 1) {
										for (int i = 1; i <= totalPage; i++) {
											if (i == 1) {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>">[FirstPage]</a> </span>
								<%
									} else if (i == totalPage) {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>">[LastPage]</a> </span>
								<%
									} else {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>"><%=i%></a> </span>
								<%
									}
										}
									}
								%>
							</div>
							</div>
							<div id="diyList" style="display:none;">
								<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" bordercolor="#CCCCCC">
								<tr style="background-color:#FCF2E4;">
									<td width="30">
										Index
									</td>
									<td width="130">
										Code
									</td>
									<td width="60">
										Amount
									</td>
									<td width="250">
										Description
									</td>
									<td width="110">
										Address
									</td>
									<td width="90">
										Phone
									</td>
									<td width="100">
										Remark
									</td>
									<td width="60">
										Status
									</td>
									<td width="110">
										Operation
									</td>
								</tr>
								<%

									sql = "select * from diy order by status asc";

									rs = db.query(sql);
									index = 1;
									while (rs.next()) {
								%>
								<tr>
									<td><%=index%></td>
									<td><%=rs.getString("code")%></td>
									<td><%=rs.getFloat("amount")%></td>
									<td>
										<a href="<%=rs.getString("picture")%>" target="blank"><img src="<%=rs.getString("picture")%>"
											alt="diy"
											style="float: left; width: 100px; height: 100px;" /></a>
											<%=rs.getString("description") %>
									</td>
									<td><%=rs.getString("address")%></td>
									<td><%=rs.getString("phone")%></td>
									<td><%=rs.getString("remark")%></td>
									<td>
										<%
											int status = rs.getInt("status");
												String temp = "";
												String operation="";
												switch (status) {
												case 0 :
													temp = "ordered";
													operation = "<span class='pointer' onclick='sendDiyOrder("+rs.getLong("id")+");'>send</span>"
															+" | <span class='pointer' onclick='cancelDiyOrderByAdmin("+rs.getLong("id")+");'>cancel</span>";
													break;
												case 1 :
													temp = "sended";
													break;
												case 2 :
													temp = "successed";
													break;
												case 3 :
													temp = "failed";
													break;
												}
										%>
										<%=temp%>
									</td>
									<td>
										<%=operation %>
									</td>
								</tr>
								<%
									index++;
									}
								%>
							</table>
							</div>
						</div>
					</div>
				</div>
				<div id="categorys">
					<div style="margin-top:30px;">
						<hr />
						<div style="margin-left:250px;">
							Address：Beijing chaoyang district pingleyuan 100   phone：400-678-5618<br/>  
							Email： <a href=mailto:sample@163.com>a87980011@163.com</a> <br/>
							All Rights Reserved.：Laomo（jingsong branch）<br/>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
